import { queryInstDeatilInfo, queryInstOrderInfo } from '../../services/inspect-project'
import { queryOrderById } from '../../services/order'
import { queryVehicleDetail } from '../../services/vehicle'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    instId:"",
    orderId:"",
    inspectList:[],
    inspectItem:{},
    pgResult:[],
    basicInfo:{},
    basicList:[
      // {
      //   label:"测试设备",
      //   value:"绝缘检测装置"
      // },
      {
        label:"检测地址",
        value:"南京市雨花台区"
      },
      {
        label:"环境温度",
        value:"30℃"
      },
      {
        label:"相对湿度",
        value:"66%"
      },
      {
        label:"大气压力",
        value:"100kPa"
      },
      {
        label:"开始时间",
        value:"2023-06-30 14:58:12"
      },
      {
        label:"结束时间",
        value:"2023-06-30 14:58:12"
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(!options){
      return
    }

    let instId = options.instId
    let orderId = options.orderId
    this.getInstDeatilInfo(instId)
    this.getOrderDetailInfo(orderId)
    this.setData({
      instId,
      orderId,
      ['basicInfo.reportId']: instId,
      ['basicInfo.orderId']: orderId
    })
  },

  onShow(){

  },

  async getInstDeatilInfo(instId){
    let res = await queryInstDeatilInfo(instId)
    if(res.code === 200){
      let inspectItem = res.data
      let inspectList = JSON.parse(inspectItem.itemJson)
      this.setData({
        inspectItem,
        inspectList,
        ['basicInfo.reportDate']: inspectItem.updateTime,
        ['basicInfo.detectStartTime']: inspectItem.detectStartTime || "",
        ['basicInfo.detectEndTime']: inspectItem.detectEndTime || "",
      })
      this.setBasicData(inspectItem, this.data.orderInfo)
      this.setPgResult(inspectList)
    }
  },

  async getOrderDetailInfo(orderId){
    let orderRes = await queryOrderById(orderId)
    if(orderRes.code === 200){
      let orderInfo = orderRes.data
      let { vehicleId, plateNo } = orderInfo
      this.setData({
        orderInfo,
        ['basicInfo.plateNo']: plateNo || ""
      })
      this.setBasicData(this.data.inspectItem, orderInfo)
      if(vehicleId){
        this.getVehicleDetailInfo(vehicleId)
      }
    }
  },

  async getVehicleDetailInfo(vehicleId){
    let res = await queryVehicleDetail(vehicleId)
    if(res.code === 200){
      let vehicleInfo = res.data
      this.setData({
        vehicleInfo,
        ['basicInfo.vin']: vehicleInfo.vin || ""
      })
    }
  },

  setBasicData(instInfo = {}, orderInfo = {}){
    let weatherInfo = instInfo.desc ? JSON.parse(instInfo.desc) : {}
    let airTemperature = weatherInfo.airTemperature || ""
    let humidity = weatherInfo.humidity || ""
    let pressure = weatherInfo.pressure || ""

    let basicList = [
      {
        label:"检测地址",
        value: orderInfo.stationName || ""
      },
      {
        label:"环境温度",
        value: airTemperature ? airTemperature + '℃' : ''   
      },
      {
        label:"相对湿度",
        value: humidity ? humidity + '%' : '' 
      },
      {
        label:"大气压力",
        value: pressure ? pressure + 'kPa' : ''
      },
      {
        label:"开始时间",
        value: instInfo.detectStartTime || ""
      },
      {
        label:"结束时间",
        value: instInfo.detectEndTime || ""
      },
    ]

    this.setData({
      basicList
    })
  },

  setPgResult(inspectList){
    let pgResult = []
    let pgRes1 = '绝缘性能合格'
    let pgRes2 = '耐压测试合格'

    if(inspectList && inspectList.length == 2){
      let DcInfoList = inspectList[0]['infoList']
      let AcInfoList = inspectList[1]['infoList']

      let dcHeader = '直流充电口'
      DcInfoList.forEach(item => {
        if(item.checkRes == '不合格'){
          if(item.tag == 1){
            pgRes1 = dcHeader + '-' + item.name + '-' + item.outputValue + '-' + '不合格'
          } else {
            pgRes2 = dcHeader + '-' + item.name + '-' + item.outputValue + '-' + '不合格'
          }
        }
      })

      let acHeader = '交流充电口'
      AcInfoList.forEach(item => {
        if(item.checkRes == '不合格'){
          if(item.tag == 1){
            pgRes1 = acHeader + '-' + item.name + '-' + item.outputValue + '-' + '不合格'
          } else {
            pgRes2 = acHeader + '-' + item.name+ '-' + item.outputValue + '-' + '不合格'
          }
        }
      })
    }

    pgResult.push(pgRes1)
    pgResult.push(pgRes2)

    this.setData({
      pgResult
    })
  }
})